package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.domain.UserVO;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    UserService userService;
    @GetMapping("/toAdminuser")
    public String toAdminuser(Model model){
        List<User> userList = userService.findAll();
        model.addAttribute("userlist",userList);
        return  "admin/adminuser";
    }

    /*
    参数绑定PO对象的时候注意不要使用 RequestParam注解
     */
    @PostMapping("/search")
    public String searchList(UserVO userVO,Model model) {
        System.out.println();
        List<User> userList = userService.listsearch(userVO);
        System.out.println(userList);
        model.addAttribute("userlist",userList);
        //返回给前端页面显示
        model.addAttribute("username",userVO.getUsername());
        model.addAttribute("password",userVO.getPassword());

        return "admin/adminuser";
    }

    @RequestMapping("/searchj")
    @ResponseBody
    public Map<String, Object> searchListJ( UserVO userVO) throws Exception {

        List<User> userList = userService.listsearch(userVO);
        //System.out.println(userList);
        //model.addAttribute("userlist",userList);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("data", userList);
        resultMap.put("count", userList.size());//此处放入总的数据数量
        System.out.println(resultMap);
        return resultMap;

    }

}

